/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.bank;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.web.BaseController;
import com.server.common.utils.StringUtils;
import com.server.modules.sys.entity.bank.Bank;
import com.server.modules.sys.service.bank.BankService;

/**
 * 银行信息Controller
 * @author 颜洪毅
 * @version 2016-10-18
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/bank/bank")
public class BankController extends BaseController {

	@Autowired
	private BankService bankService;
	
	@ModelAttribute
	public Bank get(@RequestParam(required=false) String id) {
		Bank entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bankService.get(id);
		}
		if (entity == null){
			entity = new Bank();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:bank:bank:view")
	@RequestMapping(value = {"list", ""})
	public String list(Bank bank, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Bank> page = bankService.findPage(new Page<Bank>(request, response), bank); 
		model.addAttribute("page", page);
		model.addAttribute("bank", bank);
		return "modules/sys/bank/bankList";
	}

	@RequiresPermissions("sys:bank:bank:view")
	@RequestMapping(value = "form")
	public String form(Bank bank, Model model) {
		model.addAttribute("bank", bank);
		return "modules/sys/bank/bankForm";
	}

	@RequiresPermissions("sys:bank:bank:edit")
	@RequestMapping(value = "save")
	public String save(Bank bank, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bank)){
			return form(bank, model);
		}
		bankService.save(bank);
		addMessage(redirectAttributes, "保存银行信息成功");
		return "redirect:"+Global.getAdminPath()+"/sys/bank/bank/?repage";
	}
	
	@RequiresPermissions("sys:bank:bank:edit")
	@RequestMapping(value = "delete")
	public String delete(Bank bank, RedirectAttributes redirectAttributes) {
		bankService.delete(bank);
		addMessage(redirectAttributes, "删除银行信息成功");
		return "redirect:"+Global.getAdminPath()+"/sys/bank/bank/?repage";
	}

}